package com.app.newcsc.fragment.my.exchangepoints;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.newcsc.R;
import com.app.newcsc.app.AppApplication;
import com.app.newcsc.entity.ConfigEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: woking
 * @date: 2021/8/3
 * @description：
 */
public class ExchangeLevelAdapter extends BaseAdapter {
    private List<ConfigEntity.LevelInfo> mDatas = new ArrayList<>();
    private Context mContext = null;

    public ExchangeLevelAdapter(Context context, List<ConfigEntity.LevelInfo> datas) {
        mDatas = datas;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public ConfigEntity.LevelInfo getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    public void setSelect(int index)
    {
        if(mDatas != null && mDatas.size() > 0)
        {
            for(int i = 0; i < mDatas.size(); i++)
            {
                if(index == i)
                {
                    mDatas.get(i).isSelect = true;
                }
                else
                {
                    mDatas.get(i).isSelect = false;
                }
            }

            notifyDataSetChanged();
        }
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_exchangelevel_list, null);
            vh.ad_img = (ImageView) view.findViewById(R.id.ad_img);
            vh.tv_name = (TextView) view.findViewById(R.id.tv_name);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        if(mDatas != null && i < mDatas.size())
        {
            vh.tv_name.setText(mDatas.get(i).info);
            if(mDatas.get(i).isSelect)
            {
                vh.ad_img.setImageResource(R.mipmap.icon_collection_check_sel);
            }
            else
            {
                vh.ad_img.setImageResource(R.mipmap.icon_collection_check_default);
            }
            int level = Integer.parseInt(mDatas.get(i).level);
            if(AppApplication.ad_exchange_level > level)
            {
                vh.tv_name.setTextColor(mContext.getResources().getColor(R.color.my_color_999999));
            }
            else
            {
                vh.tv_name.setTextColor(mContext.getResources().getColor(R.color.my_color_333333));
            }
        }
        return view;
    }

    public final class ViewHolder {
        public ImageView ad_img;
        public TextView tv_name;
    }
}
